package life.mahu.community.controller;

import life.mahu.community.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${github.client.id}")
    private String githubClientId;

    @Value("${mayun.client.id}")
    private String mayunClientId;

    @GetMapping("/isExit/{username}")
    @ResponseBody
    public Integer checkUserNameIsExit(@PathVariable("username") String username) {
        return userService.isExist(username) ? 1 : 0;
    }

    @PostMapping("/password/check")
    @ResponseBody
    public Integer checkPassword(@RequestParam("username") String username,
                                 @RequestParam("password") String password) {
        return userService.checkPassword(username, password) ? 1 : 0;
    }

    @PostMapping("/bind")
    @ResponseBody
    public void bindExistingUser(@RequestParam("username") String username,
                                 @RequestParam("accountId") Long accountId,
                                 @RequestParam("accountAvatar") String accountAvatar,
                                 @RequestParam("accountName") String accountName,
                                 @RequestParam("htmlUrl") String htmlUrl,
                                 @RequestParam("witchGit") String witchGit) {
        userService.bind(username, accountId, htmlUrl, accountAvatar, accountName, witchGit);
    }

    @PostMapping("/login")
    @ResponseBody
    public Integer userLogin(@RequestParam("username") String username,
                             @RequestParam("password") String password,
                             HttpServletResponse response) {
        String token = UUID.randomUUID().toString();
        int result = userService.login(username, 0, token, password, null);
        if (result == 2) {
            Cookie cookie = new Cookie("token", token);
            cookie.setMaxAge(60 * 60 * 24 * 30 * 6);
            cookie.setPath("/");//设置作用域,如果不设置cookie的path就是/user,那么去请求/user的父亲和兄弟路径就不会带上该cookie
            response.addCookie(cookie);
        }
        return result;
    }

    @PostMapping("/register")
    @ResponseBody
    public Integer userRegister(@RequestParam("username") String username,
                                @RequestParam("password") String password,
                                @RequestParam("name") String name) {
        return userService.register(username, password, name);
    }


    @GetMapping("/registerPage")
    public String registerPage() {
        return "register";
    }

    @GetMapping("/loginPage")
    public String loginChoose(Model model) {
        model.addAttribute("githubClientId", githubClientId);
        model.addAttribute("mayunClientId", mayunClientId);
        return "/login";
    }

    @PutMapping("/")
    @ResponseBody
    public Integer updateMessage(@RequestParam("id") Long id,
                                 @RequestParam("type") String type,
                                 @RequestParam("newValue") String newValue) {
        return userService.updateMessage(id, type, newValue);
    }

    @PutMapping("/psw")
    @ResponseBody
    public Integer updatePassword(@RequestParam("username") String username,
                                  @RequestParam("oldPassword") String oldPassword,
                                  @RequestParam("newPassword") String newPassword) {
        return userService.updatePassword(username, oldPassword, newPassword);
    }

    @PutMapping("/unbind")
    @ResponseBody
    public Integer unbind(@RequestParam("id") Long id,
                          @RequestParam("witchGit") Integer witchGit) {
        return userService.unbind(id, witchGit);
    }

}
